home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / qe4 / cmdlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-12  |  9.1 KB  |  665 lines

  1. // cmdlib.c
  2.  
  3. #include "cmdlib.h"
  4.  
  5. #define PATHSEPERATOR   '/'
  6.  
  7. char        com_token[1024];
  8. qboolean    com_eof;
  9.  
  10. /*
  11. ================
  12. I_FloatTime
  13. ================
  14. */
  15. double I_FloatTime (void)
  16. {
  17.     time_t    t;
  18.     
  19.     time (&t);
  20.     
  21.     return t;
  22. #if 0
  23. // more precise, less portable
  24.     struct timeval tp;
  25.     struct timezone tzp;
  26.     static int        secbase;
  27.  
  28.     gettimeofday(&tp, &tzp);
  29.     
  30.     if (!secbase)
  31.     {
  32.         secbase = tp.tv_sec;
  33.         return tp.tv_usec/1000000.0;
  34.     }
  35.     
  36.     return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
  37. #endif
  38. }
  39.  
  40.  
  41. /*
  42. ==============
  43. COM_Parse
  44.  
  45. Parse a token out of a string
  46. ==============
  47. */
  48. char *COM_Parse (char *data)
  49. {
  50.     int        c;
  51.     int        len;
  52.     
  53.     len = 0;
  54.     com_token[0] = 0;
  55.     
  56.     if (!data)
  57.         return NULL;
  58.         
  59. // skip whitespace
  60. skipwhite:
  61.     while ( (c = *data) <= ' ')
  62.     {
  63.         if (c == 0)
  64.         {
  65.             com_eof = true;
  66.             return NULL;            // end of file;
  67.         }
  68.         data++;
  69.     }
  70.     
  71. // skip // comments
  72.     if (c=='/' && data[1] == '/')
  73.     {
  74.         while (*data && *data != '\n')
  75.             data++;
  76.         goto skipwhite;
  77.     }
  78.     
  79.  
  80. // handle quoted strings specially
  81.     if (c == '\"')
  82.     {
  83.         data++;
  84.         do
  85.         {
  86.             c = *data++;
  87.             if (c=='\"')
  88.             {
  89.                 com_token[len] = 0;
  90.                 return data;
  91.             }
  92.             com_token[len] = c;
  93.             len++;
  94.         } while (1);
  95.     }
  96.  
  97. // parse single characters
  98.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  99.     {
  100.         com_token[len] = c;
  101.         len++;
  102.         com_token[len] = 0;
  103.         return data+1;
  104.     }
  105.  
  106. // parse a regular word
  107.     do
  108.     {
  109.         com_token[len] = c;
  110.         data++;
  111.         len++;
  112.         c = *data;
  113.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  114.             break;
  115.     } while (c>32);
  116.     
  117.     com_token[len] = 0;
  118.     return data;
  119. }
  120.  
  121.  
  122. int Q_strncasecmp (char *s1, char *s2, int n)
  123. {
  124.     int        c1, c2;
  125.     
  126.     while (1)
  127.     {
  128.         c1 = *s1++;
  129.         c2 = *s2++;
  130.  
  131.         if (!n--)
  132.             return 0;        // strings are equal until end point
  133.         
  134.         if (c1 != c2)
  135.         {
  136.             if (c1 >= 'a' && c1 <= 'z')
  137.                 c1 -= ('a' - 'A');
  138.             if (c2 >= 'a' && c2 <= 'z')
  139.                 c2 -= ('a' - 'A');
  140.             if (c1 != c2)
  141.                 return -1;        // strings not equal
  142.         }
  143.         if (!c1)
  144.             return 0;        // strings are equal
  145.     }
  146.     
  147.     return -1;
  148. }
  149.  
  150. int Q_strcasecmp (char *s1, char *s2)
  151. {
  152.     return Q_strncasecmp (s1, s2, 99999);
  153. }
  154.  
  155.  
  156.  
  157. /*
  158. =============================================================================
  159.  
  160.                         MISC FUNCTIONS
  161.  
  162. =============================================================================
  163. */
  164.  
  165.  
  166. int        argc;
  167. char    *argv[MAX_NUM_ARGVS];
  168.  
  169. /*
  170. ============
  171. ParseCommandLine
  172. ============
  173. */
  174. void ParseCommandLine (char *lpCmdLine)
  175. {
  176.     argc = 1;
  177.     argv[0] = "programname";
  178.  
  179.     while (*lpCmdLine && (argc < MAX_NUM_ARGVS))
  180.     {
  181.         while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
  182.             lpCmdLine++;
  183.  
  184.         if (*lpCmdLine)
  185.         {
  186.             argv[argc] = lpCmdLine;
  187.             argc++;
  188.  
  189.             while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
  190.                 lpCmdLine++;
  191.  
  192.             if (*lpCmdLine)
  193.             {
  194.                 *lpCmdLine = 0;
  195.                 lpCmdLine++;
  196.             }
  197.             
  198.         }
  199.     }
  200. }
  201.  
  202.  
  203.  
  204. /*
  205. =================
  206. CheckParm
  207.  
  208. Checks for the given parameter in the program's command line arguments
  209. Returns the argument number (1 to argc-1) or 0 if not present
  210. =================
  211. */
  212. int CheckParm (char *check)
  213. {
  214.     int             i;
  215.  
  216.     for (i = 1;i<argc;i++)
  217.     {
  218.         if ( !Q_strcasecmp(check, argv[i]) )
  219.             return i;
  220.     }
  221.  
  222.     return 0;
  223. }
  224.  
  225.  
  226.  
  227. /*
  228. ================
  229. Q_filelength
  230. ================
  231. */
  232. int Q_filelength (FILE *f)
  233. {
  234.     int        pos;
  235.     int        end;
  236.  
  237.     pos = ftell (f);
  238.     fseek (f, 0, SEEK_END);
  239.     end = ftell (f);
  240.     fseek (f, pos, SEEK_SET);
  241.  
  242.     return end;
  243. }
  244.  
  245.  
  246. FILE *SafeOpenWrite (char *filename)
  247. {
  248.     FILE    *f;
  249.  
  250.     f = fopen(filename, "wb");
  251.  
  252.     if (!f)
  253.         Error ("Error opening %s: %s",filename,strerror(errno));
  254.  
  255.     return f;
  256. }
  257.  
  258. FILE *SafeOpenRead (char *filename)
  259. {
  260.     FILE    *f;
  261.  
  262.     f = fopen(filename, "rb");
  263.  
  264.     if (!f)
  265.         Error ("Error opening %s: %s",filename,strerror(errno));
  266.  
  267.     return f;
  268. }
  269.  
  270.  
  271. void SafeRead (FILE *f, void *buffer, int count)
  272. {
  273.     if ( (int)fread (buffer, 1, count, f) != count)
  274.         Error ("File read failure");
  275. }
  276.  
  277.  
  278. void SafeWrite (FILE *f, void *buffer, int count)
  279. {
  280.     if ( (int)fwrite (buffer, 1, count, f) != count)
  281.         Error ("File read failure");
  282. }
  283.  
  284.  
  285.  
  286. /*
  287. ==============
  288. LoadFile
  289. ==============
  290. */
  291. int    LoadFile (char *filename, void **bufferptr)
  292. {
  293.     FILE    *f;
  294.     int    length;
  295.     void    *buffer;
  296.     extern void *qmalloc( size_t size );
  297.  
  298.     f = fopen (filename, "rb");
  299.     if (!f)
  300.     {
  301.         *bufferptr = NULL;
  302.         return -1;
  303.     }
  304.     length = Q_filelength (f);
  305.     buffer = qmalloc (length+1);
  306.     ((char *)buffer)[length] = 0;
  307.     SafeRead (f, buffer, length);
  308.     fclose (f);
  309.  
  310.     *bufferptr = buffer;
  311.     return length;
  312. }
  313.  
  314.  
  315. /*
  316. ==============
  317. LoadFileNoCrash
  318.  
  319. returns -1 length if not present
  320. ==============
  321. */
  322. int    LoadFileNoCrash (char *filename, void **bufferptr)
  323. {
  324.     FILE    *f;
  325.     int    length;
  326.     void    *buffer;
  327.  
  328.     f = fopen (filename, "rb");
  329.     if (!f)
  330.         return -1;
  331.     length = Q_filelength (f);
  332.     buffer = qmalloc (length+1);
  333.     ((char *)buffer)[length] = 0;
  334.     SafeRead (f, buffer, length);
  335.     fclose (f);
  336.  
  337.     *bufferptr = buffer;
  338.     return length;
  339. }
  340.  
  341.  
  342. /*
  343. ==============
  344. SaveFile
  345. ==============
  346. */
  347. void    SaveFile (char *filename, void *buffer, int count)
  348. {
  349.     FILE    *f;
  350.  
  351.     f = SafeOpenWrite (filename);
  352.     SafeWrite (f, buffer, count);
  353.     fclose (f);
  354. }
  355.  
  356.  
  357.  
  358. void DefaultExtension (char *path, char *extension)
  359. {
  360.     char    *src;
  361. //
  362. // if path doesn't have a .EXT, append extension
  363. // (extension should include the .)
  364. //
  365.     src = path + strlen(path) - 1;
  366.  
  367.     while (*src != PATHSEPERATOR && src != path)
  368.     {
  369.         if (*src == '.')
  370.             return;                 // it has an extension
  371.         src--;
  372.     }
  373.  
  374.     strcat (path, extension);
  375. }
  376.  
  377.  
  378. void DefaultPath (char *path, char *basepath)
  379. {
  380.     char    temp[128];
  381.  
  382.     if (path[0] == PATHSEPERATOR)
  383.         return;                   // absolute path location
  384.     strcpy (temp,path);
  385.     strcpy (path,basepath);
  386.     strcat (path,temp);
  387. }
  388.  
  389.  
  390. void    StripFilename (char *path)
  391. {
  392.     int             length;
  393.  
  394.     length = strlen(path)-1;
  395.     while (length > 0 && path[length] != PATHSEPERATOR)
  396.         length--;
  397.     path[length] = 0;
  398. }
  399.  
  400. void    StripExtension (char *path)
  401. {
  402.     int             length;
  403.  
  404.     length = strlen(path)-1;
  405.     while (length > 0 && path[length] != '.')
  406.     {
  407.         length--;
  408.         if (path[length] == '/')
  409.             return;        // no extension
  410.     }
  411.     if (length)
  412.         path[length] = 0;
  413. }
  414.  
  415.  
  416. /*
  417. ====================
  418. Extract file parts
  419. ====================
  420. */
  421. void ExtractFilePath (char *path, char *dest)
  422. {
  423.     char    *src;
  424.  
  425.     src = path + strlen(path) - 1;
  426.  
  427. //
  428. // back up until a \ or the start
  429. //
  430.     while (src != path && *(src-1) != PATHSEPERATOR)
  431.         src--;
  432.  
  433.     memcpy (dest, path, src-path);
  434.     dest[src-path] = 0;
  435. }
  436.  
  437. void ExtractFileName (char *path, char *dest)
  438. {
  439.     char    *src;
  440.  
  441.     src = path + strlen(path) - 1;
  442.  
  443. //
  444. // back up until a \ or the start
  445. //
  446.     while (src != path && *(src-1) != '/' 
  447.          && *(src-1) != '\\' )
  448.         src--;
  449.  
  450.     while (*src)
  451.     {
  452.         *dest++ = *src++;
  453.     }
  454.     *dest = 0;
  455. }
  456.  
  457. void ExtractFileBase (char *path, char *dest)
  458. {
  459.     char    *src;
  460.  
  461.     src = path + strlen(path) - 1;
  462.  
  463. //
  464. // back up until a \ or the start
  465. //
  466.     while (src != path && *(src-1) != '/' 
  467.          && *(src-1) != '\\' )
  468.         src--;
  469.  
  470.     while (*src && *src != '.')
  471.     {
  472.         *dest++ = *src++;
  473.     }
  474.     *dest = 0;
  475. }
  476.  
  477. void ExtractFileExtension (char *path, char *dest)
  478. {
  479.     char    *src;
  480.  
  481.     src = path + strlen(path) - 1;
  482.  
  483. //
  484. // back up until a . or the start
  485. //
  486.     while (src != path && *(src-1) != '.')
  487.         src--;
  488.     if (src == path)
  489.     {
  490.         *dest = 0;    // no extension
  491.         return;
  492.     }
  493.  
  494.     strcpy (dest,src);
  495. }
  496.  
  497.  
  498. /*
  499. ==============
  500. ParseNum / ParseHex
  501. ==============
  502. */
  503. int ParseHex (char *hex)
  504. {
  505.     char    *str;
  506.     int    num;
  507.  
  508.     num = 0;
  509.     str = hex;
  510.  
  511.     while (*str)
  512.     {
  513.         num <<= 4;
  514.         if (*str >= '0' && *str <= '9')
  515.             num += *str-'0';
  516.         else if (*str >= 'a' && *str <= 'f')
  517.             num += 10 + *str-'a';
  518.         else if (*str >= 'A' && *str <= 'F')
  519.             num += 10 + *str-'A';
  520.         else
  521.             Error ("Bad hex number: %s",hex);
  522.         str++;
  523.     }
  524.  
  525.     return num;
  526. }
  527.  
  528.  
  529. int ParseNum (char *str)
  530. {
  531.     if (str[0] == '$')
  532.         return ParseHex (str+1);
  533.     if (str[0] == '0' && str[1] == 'x')
  534.         return ParseHex (str+2);
  535.     return atol (str);
  536. }
  537.  
  538.  
  539.  
  540. /*
  541. ============================================================================
  542.  
  543.                     BYTE ORDER FUNCTIONS
  544.  
  545. ============================================================================
  546. */
  547.  
  548. #ifdef _SGI_SOURCE
  549. #define    __BIG_ENDIAN__
  550. #endif
  551.  
  552. #ifdef __BIG_ENDIAN__
  553.  
  554. short   LittleShort (short l)
  555. {
  556.     byte    b1,b2;
  557.  
  558.     b1 = l&255;
  559.     b2 = (l>>8)&255;
  560.  
  561.     return (b1<<8) + b2;
  562. }
  563.  
  564. short   BigShort (short l)
  565. {
  566.     return l;
  567. }
  568.  
  569.  
  570. int    LittleLong (int l)
  571. {
  572.     byte    b1,b2,b3,b4;
  573.  
  574.     b1 = l&255;
  575.     b2 = (l>>8)&255;
  576.     b3 = (l>>16)&255;
  577.     b4 = (l>>24)&255;
  578.  
  579.     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  580. }
  581.  
  582. int    BigLong (int l)
  583. {
  584.     return l;
  585. }
  586.  
  587.  
  588. float    LittleFloat (float l)
  589. {
  590.     union {byte b[4]; float f;} in, out;
  591.     
  592.     in.f = l;
  593.     out.b[0] = in.b[3];
  594.     out.b[1] = in.b[2];
  595.     out.b[2] = in.b[1];
  596.     out.b[3] = in.b[0];
  597.     
  598.     return out.f;
  599. }
  600.  
  601. float    BigFloat (float l)
  602. {
  603.     return l;
  604. }
  605.  
  606.  
  607. #else
  608.  
  609.  
  610. short   BigShort (short l)
  611. {
  612.     byte    b1,b2;
  613.  
  614.     b1 = l&255;
  615.     b2 = (l>>8)&255;
  616.  
  617.     return (b1<<8) + b2;
  618. }
  619.  
  620. short   LittleShort (short l)
  621. {
  622.     return l;
  623. }
  624.  
  625.  
  626. int    BigLong (int l)
  627. {
  628.     byte    b1,b2,b3,b4;
  629.  
  630.     b1 = l&255;
  631.     b2 = (l>>8)&255;
  632.     b3 = (l>>16)&255;
  633.     b4 = (l>>24)&255;
  634.  
  635.     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  636. }
  637.  
  638. int    LittleLong (int l)
  639. {
  640.     return l;
  641. }
  642.  
  643. float    BigFloat (float l)
  644. {
  645.     union {byte b[4]; float f;} in, out;
  646.     
  647.     in.f = l;
  648.     out.b[0] = in.b[3];
  649.     out.b[1] = in.b[2];
  650.     out.b[2] = in.b[1];
  651.     out.b[3] = in.b[0];
  652.     
  653.     return out.f;
  654. }
  655.  
  656. float    LittleFloat (float l)
  657. {
  658.     return l;
  659. }
  660.  
  661.  
  662.  
  663. #endif
  664.  
  665.